package com.example.mymusicplayer;

import static com.example.mymusicplayer.MainActivity.musicFiles;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class AlbumContents extends AppCompatActivity {
    ImageView AlbumCover;
    static ArrayList<MusicFiles> TheAlbum ;
    RecyclerView recyclerView;
    String AlbumName;
AlbumAdapterDetail albumAdapterDetail;
    @Override
    protected void onResume() {
        super.onResume();
        System.out.println(TheAlbum);
        if(!(TheAlbum.size()<1)){
            albumAdapterDetail = new AlbumAdapterDetail(this,TheAlbum);
            recyclerView.setAdapter(albumAdapterDetail);
            recyclerView.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_album_contents);
        recyclerView = findViewById(R.id.DetailView);
        AlbumCover = findViewById(R.id.AlbumCover);
        AlbumName = getIntent().getStringExtra("AlbumName");
        TheAlbum = new ArrayList<>();
        int j =0;
        for(int i=0;i<musicFiles.size();i++){
            if(AlbumName.equals(musicFiles.get(i).getAlbum())){
                TheAlbum.add(j,musicFiles.get(i));
                j++;
            }
        }
        byte[] image = getAlbumAct(TheAlbum.get(0).getPath());
        if(image!=null){
            Glide.with(this).load(image).into(AlbumCover);
        }
        else
        {
            Glide.with(this).load(R.drawable.f9dcd100baa1cd11728b2080fc5edffcc3cec3fd8775).into(AlbumCover);
        }
    }
    private byte[] getAlbumAct(String uri){
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try{
            retriever.setDataSource(uri);
        }
        catch(java.lang.RuntimeException e){
            return null;
        }
        byte[] art = retriever.getEmbeddedPicture();
        retriever.release();
        return art;
    }
    }
